package com.power.services.member;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.power.dao.member.TmMemberDAO;
import com.power.dao.user.SysuserDAO;
import com.power.model.member.TmMember;
import com.power.model.user.TsSysuser;

@Service("tmMemberService")
public class TmMemberServiceImpl implements TmMemberService {
	@Autowired
	private TmMemberDAO tmMemberdao;
	@Autowired
	private SysuserDAO tsSysuserdao;

	public void create(TmMember tmMember) {
		tmMemberdao.save(tmMember);

	}

	public void delete(TmMember tmMember) {
		tmMemberdao.delete(tmMember);

	}

	public TmMember read(Integer id) {
		return tmMemberdao.load(id);

	}

	public void update(TmMember tmMember) {
		tmMemberdao.update(tmMember);

	}

	public void createTmMember(TmMember tmMember, TsSysuser tsSysuser,
			TsSysuser loginUser) {
		if("超级管理员".equals(tsSysuser.getUserGroup()))
			tsSysuser.setUserGroup("1");
		else if("管理员".equals(tsSysuser.getUserGroup()))
			tsSysuser.setUserGroup("2");
		else if("服务人员".equals(tsSysuser.getUserGroup()))
			tsSysuser.setUserGroup("3");
		tsSysuserdao.saveOrUpdate(tsSysuser);
		tmMember.setLoginId(tsSysuser.getId());
		tmMember.setInputMan(loginUser.getUsername());
		tmMember.setInputManId(loginUser.getId());
		tmMemberdao.saveOrUpdate(tmMember);
	}

	public Map<String, Object> findTmMember(Map<String, String> paramMap) {
		Map<String, Object> restulMap = new HashMap<String, Object>();
		int start = Integer.parseInt(paramMap.get("start"));
		int limit = Integer.parseInt(paramMap.get("limit"));
		String category = paramMap.get("category");
		String str = " select  tm,ts   from TmMember tm,TsSysuser ts where  tm.loginId = ts.id ";
		if (null != paramMap.get("loginName")
				&& !"".equals(paramMap.get("loginName"))) {
			str += "  and  ts.username like '" + paramMap.get("loginName")
					+ "%'";
		}
		str += " and ts.category = '"+category+"' ";
		List<Object[]> data = tmMemberdao.find(str, start, limit);
		List<Object[]> countData = tmMemberdao.find(str);
		int count = countData == null ? 0 : countData.size();
		restulMap.put("data", data);
		restulMap.put("count", count);
		return restulMap;

	}

	public void deleteMember(String deleteIds) {
		String[] ids = deleteIds.split("_");
		for (int i = 0; i < ids.length; i++) {
//			TmMember tm = tmMemberdao.load(Integer.parseInt(ids[i]));
			tsSysuserdao.executeSql("delete from  t_s_sysuser where id ="
					+ ids[i]);
			tmMemberdao.executeSql(" delete from  t_m_member where login_id ="
					+ ids[i]);

		}
	}

	public void updateMemberLock(String id, String flag) {
		tsSysuserdao.executeSql(" update t_s_sysuser set islock =  " + flag
				+ " where id=" + id);
	}

	public void updatePassword(String id, String password) {
		tsSysuserdao.executeSql(" update t_s_sysuser set password =  '"
				+ password + "' where id=" + id);
	}

	public Integer createRegisterTmMember(TmMember tmMember, TsSysuser tsSysuser) {
		tsSysuserdao.saveOrUpdate(tsSysuser);
		tmMember.setLoginId(tsSysuser.getId());
		tmMember.setInputMan(null);
		tmMember.setInputManId(null);
		tmMemberdao.saveOrUpdate(tmMember);
		return tsSysuser.getId();
	}

	@SuppressWarnings("unchecked")
	public List<TmMember> findEmailExisist(String email) {
		return tmMemberdao.find("select tm from  TmMember tm where tm.email='"
				+ email + "'");
	}

	@SuppressWarnings("unchecked")
	public TsSysuser findEmailByUser(String email) {
		List<TmMember> tmList = tmMemberdao
				.find("select tm from  TmMember tm where tm.email='" + email
						+ "'");
		if (tmList != null && !tmList.isEmpty()) {
			return tsSysuserdao.load(tmList.get(0).getLoginId());
		}
		return null;
	}

	public TmMember findTmMemberById(String id) {
		List<TmMember> list = tmMemberdao
		.find("select d from com.power.model.member.TmMember d  where  d.loginId='"
				+ id + "'");
		if(null!=list&&list.size()>0)
			return list.get(0);
		return null;
	}
	public void updateLoginPassword(String id, String password) {
		tsSysuserdao.executeSql(" update t_s_sysuser set password =  '"
				+ password + "' where id=" +id);
	}

	public List<TmMember> findPhoneExisist(String email) {
		return tmMemberdao.find("select tm from  TmMember tm where tm.phone='"
				+ email + "'");
	}

	public List<TmMember> findIdcardExisist(String email) {
		return tmMemberdao.find("select tm from  TmMember tm where tm.idcard='"
				+ email + "'");
	}
}
